package jse.test20160418.CompareSerializableAndClone;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Test {
	
	@org.junit.Test
	public void test() {
		
		//新建员工对象
		Emloyees emloyees = new Emloyees("康康",20);
		List<Emloyees> list = new ArrayList<Emloyees>();
		
		//得到当前系统时间
		long currentTime = System.currentTimeMillis();
		System.out.println(currentTime);
		//克隆话费时间
		for(int i = 0;i<=100;i++) {
			try {
				list.add(emloyees.clone());
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(System.currentTimeMillis());
		System.out.println("克隆话费时间："+(System.currentTimeMillis() - currentTime));
		//序列化
		System.out.println("=======================================================");
		Long time = System.currentTimeMillis();
		for(int i = 0;i<=100;i++) {
			
			//创建字节输出数组
			ByteArrayOutputStream bsos = new ByteArrayOutputStream();
			ObjectOutputStream out = null;
			try {
				out = new ObjectOutputStream(bsos);
				out.writeObject(emloyees);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			ByteArrayInputStream bais = new ByteArrayInputStream(bsos.toByteArray());
			ObjectInputStream inputStream = null; 
			
			try {
				inputStream = new ObjectInputStream(bais);
				try {
					list.add((Emloyees)inputStream.readObject());
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		System.out.println("序列化:"+(System.currentTimeMillis()-time ));
	}
}
